package com.df.tank.designPattern.singleton;

/**
 * 懒汉式
 * 使用synchronized保证线程安全
 * 效率不好
 */
public class Singleton03 {
    private static Singleton03 instance = null;
    private Singleton03() {
    }

    public  static Singleton03 getInstance() {
        synchronized (Singleton03.class) {
            if (instance == null) {
                instance = new Singleton03();
            }
        }
        return instance;
    }
    /*
    尽可能减小锁的粒度  效率比上面好点
    需要双重检查
     */
    public  static Singleton03 getInstance2() {
        if (instance == null) {
            synchronized (Singleton03.class) {
                if (instance == null) {
                    instance = new Singleton03();
                }
            }

        }

        return instance;
    }
}
